package com.lin.activity;

import com.google.zxing.common.StringUtils;
import com.lin.utils.Session;
import com.lin.utils.SharePreferenceUtil;
import com.lin.utils.Session.ResultReceiver;
import com.lin.utils.Session.SignupResult;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.bean.UserBean;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Toast;

public class ResetPasswordActivity extends Activity implements OnClickListener{

	private Button mButtonFinish;
	private EditText mEditTextNewPWDConfirm;
	private EditText mEditTextPWD;
	private EditText mEditTextOLDPWD;
	Session mSession;
	UserBean user;
	private String username;
	private String pwd;
	private FrameLayout mFrameLayoutBack;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reset_password);
		mButtonFinish = (Button) findViewById(R.id.button_finish);
		mEditTextNewPWDConfirm = (EditText) findViewById(R.id.edittext_new_pwd_confirm);
		mEditTextPWD = (EditText) findViewById(R.id.edittext_new_pwd);
		mEditTextOLDPWD = (EditText) findViewById(R.id.edittext_old_pwd);
		mButtonFinish.setOnClickListener(this);
		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);
		
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		
		mFrameLayoutBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		user = BaseActivity.getUser(getApplicationContext());
	}

	@Override
	public void onClick(View v) {
		username = SharePreferenceUtil.getInstance().getUserName(getApplicationContext());
		pwd = SharePreferenceUtil.getInstance().getPassword(getApplicationContext());
		if(!pwd.equals(mEditTextOLDPWD.getText().toString())) {
			Toast.makeText(getApplicationContext(), "密码不正确，请重新输入", Toast.LENGTH_LONG).show();
		}
		if(mEditTextPWD.getText().toString().length() < 6) {
			Toast.makeText(getApplicationContext(), "请输入大于六个字符的新密码", Toast.LENGTH_LONG).show();
		} 
		if (mEditTextNewPWDConfirm.getText().toString().length() < 6) {
			Toast.makeText(getApplicationContext(), "请输入大于六个字符的新密码", Toast.LENGTH_LONG).show();
		}
		if(!mEditTextPWD.getText().toString().equals(mEditTextNewPWDConfirm.getText().toString())) {
			Toast.makeText(getApplicationContext(), "两次输入的密码不一致", Toast.LENGTH_LONG).show();
		}
		mSession.changePass(getApplicationContext(), user.getPhone(), mEditTextNewPWDConfirm.getText().toString(), "", new ResultReceiver<SignupResult>() {

			@Override
			public void onResult(boolean last, SignupResult data) {
				// TODO Auto-generated method stub
				if(data == SignupResult.SUCCESS) {
					Toast.makeText(getApplicationContext(), "修改密码成功", Toast.LENGTH_SHORT).show();
					SharePreferenceUtil.getInstance().updateUserInfo(getApplicationContext(), username, pwd);
					finish();
				}
			}
			
		});
		
	}
	
}